#!/bin/sh

export LANG="zh_CN.UTF-8"
export PATH=/jws.mono/mono/bin:$PATH
export LD_LIBRARY_PATH=/jws.mono/mono/lib:$LD_LIBRARY_PATH
#export MONO_PATH=/jws.mono/mono/lib/mono/4.5

export MONO_IOMAP="all"
# export MONO_MANAGED_WATCHER="disable"
# export MONO_EXTERNAL_ENCODINGS="gbk:gb18030"
# export MONO_STRICT_MS_COMPLIANT="yes"
# export MONO_GC_PARAMS="max-heap-size=1024m"
# export MONO_DISABLE_AIO=1
export MONO_ASPNET_WEBCONFIG_CACHESIZE=4096
export MONO_THREADS_PER_CPU=2000



# ulimit -SHn 10240

jws_status_val=
JWS_HOME=$(cd $(dirname $0);pwd)

get_status()
{
    for i in 0 0 0 0 0 0 0 0 0 0 0
    do
      sleep 1
      pid=`cat /var/run/jexus/jws.pid 2>/dev/null`
      if [ "$pid" = "" ]; then
         continue
      fi

      jws_status_val=`ps -p $pid | grep $pid | wc -l`
      if [ $jws_status_val -ge 1 ]; then
         jws_status_val=1
         return
      fi
    done
    jws_status_val=0
}

wait_new()
{
  pid=`cat /var/run/jexus/jws.pid 2>/dev/null`
  if [ "$pid" = "" ]; then
    return
  fi

  for i in 0 0 0 0 0 0 0 0 0 0
  do
    sleep 1
    p=`cat /var/run/jexus/jws.pid 2>/dev/null`
    if [ "$p" = "" ]; then
      continue
    fi     

    if [ $pid != $p ]; then
      return
    fi
  done
}


case "$1" in

  start)
    if [ "x$2" != "x" ]; then
      mono ${JWS_HOME}/jws.exe -reapp "$2"
      exit 0
    fi
    echo -n "Starting ... "
    mono ${JWS_HOME}/jws.exe >/dev/null 2>&1 &

    wait_new
    get_status
    if [ $jws_status_val -eq 1 ]; then
      echo "OK"
    else
      echo "Failure"
    fi
    ;;

  stop)
    if [ "x$2" != "x" ]; then
      mono ${JWS_HOME}/jws.exe -stop "$2"
      exit 0
    fi
    mono ${JWS_HOME}/jws.exe -stop
    ;;

  restart)
    if [ "x$2" != "x" ]; then
      mono ${JWS_HOME}/jws.exe -reapp "$2"
      exit 0
    fi
    echo -n "Restarting ... "
    mono ${JWS_HOME}/jws.exe >/dev/null 2>&1 &

    wait_new
    get_status
    if [ $jws_status_val -eq 1 ]; then
      echo "OK"
    else
      echo "Failure"
    fi
    ;;

  regsvr)
    gacutil -u jxHost > /dev/null 2>&1
    gacutil -u jxAspx > /dev/null 2>&1
    gacutil -u jxHost2 > /dev/null 2>&1
    gacutil -u jxAspx2 > /dev/null 2>&1

    rm -f ${JWS_HOME}/jxAspx*.dll.so >/dev/null 2>&1
    mono --aot ${JWS_HOME}/jxAspx2.dll >/dev/null 2>&1
    mono --runtime=v4.0.30319 --aot ${JWS_HOME}/jxAspx.dll >/dev/null 2>&1
    gacutil -i ${JWS_HOME}/jxAspx2.dll
    gacutil -i ${JWS_HOME}/jxAspx.dll


    rm -f ${JWS_HOME}/jxHost*.dll.so >/dev/null 2>&1
    mono --aot ${JWS_HOME}/jxHost2.dll >/dev/null 2>&1
    mono --runtime=v4.0.30319 --aot ${JWS_HOME}/jxHost.dll >/dev/null 2>&1
    gacutil -i ${JWS_HOME}/jxHost2.dll
    gacutil -i ${JWS_HOME}/jxHost.dll

    rm -f ${JWS_HOME}/jwsHttpd*.exe.so >/dev/null 2>&1
    mono --aot ${JWS_HOME}/jwsHttpd2.exe >/dev/null 2>&1
    mono --runtime=v4.0.30319 --aot ${JWS_HOME}/jwsHttpd.exe >/dev/null 2>&1

    python ${JWS_HOME}/def.py > ${JWS_HOME}/os.def 2>/dev/null
    ;;

  status)
    get_status
    if [ $jws_status_val -eq 1 ]; then
      echo "Jexus is running."
      exit 0
    fi
    echo "Jexus has stopped."
    exit 3
    ;;

  -v | -V)
    mono ${JWS_HOME}/jws.exe -V
    ;;

  *)
    echo "Usage: jws {start|stop|restart|regsvr|status|-v}"
    exit 1

esac


exit 0
